package cn.dankal.flyaudio.personal

import android.view.View
import cn.dankal.basiclib.base.BaseRvActivity
import cn.dankal.basiclib.base.activity.BaseStateActivity
import cn.dankal.basiclib.base.callback.DKCallBack
import cn.dankal.basiclib.base.callback.DKCallBackObject
import cn.dankal.basiclib.base.recyclerview.BaseRecyclerViewAdapter
import cn.dankal.basiclib.base.recyclerview.BaseRecyclerViewPresenter
import cn.dankal.basiclib.base.recyclerview.OnRvItemClickListener
import cn.dankal.basiclib.protocol.MainProtocol
import cn.dankal.basiclib.util.ToastUtils
import cn.dankal.business.pojo.CurrentBean
import cn.dankal.business.Constants
import cn.dankal.business.eventbus.MessageDriverHomeEvent
import cn.dankal.business.eventbus.MessageDriverMyEvent
import cn.dankal.flyaudio.home.CurrentAdapter
import cn.dankal.flyaudio.home.CurrentPrsenter
import cn.dankal.flyaudio.home.MyCurrentPrsenter
import cn.dankal.flyaudio.personal.notificaiton.DataFactory
import com.alibaba.android.arouter.launcher.ARouter
import org.greenrobot.eventbus.EventBus
import org.greenrobot.eventbus.Subscribe
import org.greenrobot.eventbus.ThreadMode

/**
 * 我的此刻
 */
class MyCurrentActivity : BaseRvActivity<CurrentBean>(), CurrentPrsenter.PraiseCallback  {

    override fun getPresenter(): BaseRecyclerViewPresenter<CurrentBean>? {
        return MyCurrentPrsenter()
    }

    override fun getAdapter(): BaseRecyclerViewAdapter<CurrentBean>? {
        var currentAdapter = CurrentAdapter(DKCallBackObject {
            (mPresenter as MyCurrentPrsenter).praise(it, DKCallBack {})
        })
        currentAdapter.setOnParseListener(object : CurrentAdapter.OnPraiseListener {

            override fun praise(uuid: String, type: String, position: Int) {
                (mPresenter as MyCurrentPrsenter).praise(uuid, position, true, this@MyCurrentActivity)
            }

            override fun praiseCancel(uuid: String, type: String, position: Int) {
                (mPresenter as MyCurrentPrsenter).praise(uuid, position, false, this@MyCurrentActivity)
            }

            override fun deleteDate(uuids: Any, position: Int) {
                mAdapter.remove(position)
                var array: Array<String> = uuids as Array<String>
                (mPresenter as MyCurrentPrsenter).deleteInfo(array[0], position, this@MyCurrentActivity)

            }
        })
        currentAdapter.setOnRvItemClickListener(object : OnRvItemClickListener<CurrentBean>() {
            override fun onItemClick(v: View?, position: Int, data: CurrentBean) {
                ARouter.getInstance().build(MainProtocol.CURRENT_DETAIL)
                        .withString(Constants.UUID, data.uuid)
                        .navigation()

            }
        })
        return currentAdapter
    }

    override fun initComponents() {
        super.initComponents()
        addSingleTitleBar("我的此刻")

    }

    override fun onPraiseCallback(position: Int) {
        if (mAdapter.datas.size > position) {

            var likeCount: Int
            try {
                likeCount = Integer.valueOf(mAdapter.datas[position].likeNumber)
            } catch (e: Exception) {
                likeCount = 1
            }

            var tragetCurrent = mAdapter.datas.get(position)
            // 大于 0 表示为 已点赞
            if (tragetCurrent.isPraise > 0) {
                mAdapter.datas.get(position).isPraise = 0
                mAdapter.datas.get(position).likeNumber = (likeCount - 1).toString()

            } else {
                mAdapter.datas.get(position).isPraise = 1
                mAdapter.datas.get(position).likeNumber = (likeCount + 1).toString()
            }
            mAdapter.notifyItemChanged(position, "star")
        }
    }

    override fun onDetectCallback(message: String, position: Int) {
        ToastUtils.showShort(message)
    }

    override fun onStart() {
        super.onStart()
        EventBus.getDefault().register(this)
    }

    override fun onStop() {
        super.onStop()
        EventBus.getDefault().unregister(this)
    }

    @Subscribe(threadMode = ThreadMode.MAIN, sticky = true)
    fun onMessageEvent(event: MessageDriverMyEvent) {
        if (mPresenter != null)
            mPresenter.requestData(pageIndex.toString())
        mAdapter.notifyDataSetChanged()
    }
}
